from django.db import connection
from django.apps import apps


class TenantMigrationRouter:
    def __is_scoped_app(self,app_label:str)->bool:
        app_instance=apps.get_app_config(app_label)
        return getattr(app_instance,"scoped",False)
        
    def allow_migrate(self,db,app_label,model_name=None,**options):
        target_schema=connection.primary_schema

        if target_schema=='public' and not self.__is_scoped_app(app_label):
            return None
        if target_schema !='public' and self.__is_scoped_app(app_label):
            return None
        return False